import request from "@/api";
import { BaseRet, PageQuery, PageRet } from "../response";
import { FormControlType } from "@/components/DesignForm";

export default class DesignFormApi {
  /**
   * 查询
   * @returns
   */
  static async query(data: DesignFormQuery): Promise<PageRet<DesignFormModel>> {
    return await request({
      url: "/designForm/query",
      method: "post",
      data: data,
    });
  }

  /**
   * 新增
   * @returns
   */
  static async create(data: DesignFormEdit): Promise<BaseRet> {
    return await request({
      url: `/designForm/create`,
      method: "post",
      data: data,
    });
  }

  /**
   * 修改
   * @returns
   */
  static async update(data: DesignFormEdit): Promise<BaseRet> {
    return await request({
      url: `/designForm/update`,
      method: "put",
      data: data,
    });
  }

  /**
   * 删除
   * @returns
   */
  static async delete(id: string): Promise<BaseRet> {
    return await request({
      url: `/designForm/delete/${id}`,
      method: "delete",
    });
  }

  /**
   * 详情
   * @returns
   */
  static async details(id: string): Promise<DesignFormEdit> {
    return await request({
      url: `/designForm/details/${id}`,
      method: "get",
    });
  }

  /**
   * 下拉框
   * @returns
   */
  static async select(): Promise<DesignFormSelect[]> {
    return await request({
      url: `/designForm/select`,
      method: "get",
    });
  }

  /**
   * 根据表单id查询所使用的表对应的字段
   * @returns
   */
  static async queryFieldSelect(formId: string): Promise<DesignFormFieldSelect[]> {
    return await request({
      url: `/designForm/QueryFieldSelect/${formId}`,
      method: "get",
    });
  }

  /**
   * 根据表单id查询表单所有字段配置
   * @returns
   */
  static async QueryFormFieldConfig(formId: string): Promise<DesignFormField[]> {
    return await request({
      url: `/designForm/QueryFormFieldConfig/${formId}`,
      method: "get",
    });
  }
}

export interface DesignFormModel {
  id?: string;
  name?: string;
  // sourceId?: string;
  sourceCode?: string;
  sourceName?: string;
}

export interface DesignFormQuery extends PageQuery {
  searchString?: string;
}

export interface DesignFormEdit {
  id?: string;
  name?: string;
  // sourceId?: string;
  sourceCode?: string;
  sourceName?: string;
  formConfig?: string;
  formStyle?: string;
  formItemList?: string;
}

export interface DesignFormSelect {
  formId?: string;
  formName?: string;
}

export interface DesignFormFieldSelect {
  fieldName?: string;
  fieldText?: string;
  fieldType?: FormControlType;
  options?: DesignFormFieldOption[];
}

export interface DesignFormField {
  field?: string;
  dataType?: string;
  fieldType?: string;
  options?: DesignFormFieldOption[];
}

export interface DesignFormFieldOption {
  label?: string;
  value?: string;
}
